---@diagnostic disable: undefined-global

local chengmou = fk.CreateSkill{
  name = "lvshi__chengmou",
}

local ls = require "packages/lvshi/utility"
local DIY = require "packages/diy_utility/diy_utility"

-- 获取手牌花色数的辅助函数
local function getHandSuitCount(player)
  local card_ids = player:getCardIds("h")
  return ls.countSuits(card_ids)
end

local function getCenterTypeCount(room)
  local center_cards = DIY.getCenterCards(room)
  local types = {}
  for _, id in ipairs(center_cards) do
    local card = Fk:getCardById(id)
    if card then
      types[card.type] = true
    end
  end
  return #types
end

-- 使用viewas效果实现视为使用【暗度陈仓】
chengmou:addEffect("viewas", {
  card_filter = Util.FalseFunc,
  pattern = "lvshi__secret_advance",  -- 视为使用暗度陈仓
  view_as = function(self, cards)
    local card = Fk:cloneCard("lvshi__secret_advance", Card.NoSuit, 0)
    card.skillName = chengmou.name
    return card
  end,
  before_use = function(self, player, use)
    -- 记录目标角色使用前的手牌花色数
    local room = player.room
    for _, target in ipairs(use.tos) do
      local suit_count = getHandSuitCount(target)
      room:setPlayerMark(target, "lvshi__chengmou-suits-before", suit_count)
    end
    return true
  end,
  after_use = function(self, player, use)
    local room = player.room
    -- 检查每个目标角色的手牌花色数变化
    local foresight = false
    for _, target in ipairs(use.tos) do
      if not target.dead then
        local current_suit_count = getHandSuitCount(target)
        local before_suit_count = target:getMark("lvshi__chengmou-suits-before") or 0
        
        -- 如果目标手牌的花色数比结算前多，视为使用【洞烛先机】（即无中生有）
        if current_suit_count > before_suit_count then
          foresight = true
        end
        -- 清除标记
        room:setPlayerMark(target, "lvshi__chengmou-suits-before", 0)
      end
    end
    if foresight then
      room:useVirtualCard("foresight", nil, player, player, chengmou.name)
    end
  end,
  enabled_at_play = function(self, player)
    local max_times = getCenterTypeCount(Fk:currentRoom()) + 1
    local use_times = player:usedSkillTimes("lvshi__jinze") + player:usedSkillTimes("lvshi__chengmou")
    if player:hasSkill("lvshi__cunmu", true) then
      return use_times < max_times
    else
      return true
    end
  end,
})

chengmou:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(chengmou.name) and target == player and data.card.skillName == chengmou.name
  end,
  on_use = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.strategy = data.card.trueName.."_scheme"
  end,
})

Fk:loadTranslationTable{
  ["lvshi__chengmou"] = "逞谋",
  [":lvshi__chengmou"] = "出牌阶段，你可视为使用智策略【暗度陈仓】，结算后若目标手牌的花色数多于结算前，你视为使用【洞烛先机】。",
  
  ["#chengmou-invoke"] = "逞谋：是否视为使用智策略【暗度陈仓】？",
  ["#chengmou-choose-target"] = "逞谋：选择一名角色作为【暗度陈仓】的目标",

  ["$lvshi__chengmou1"] = "官渡之战我在场，火烧乌巢我指挥。",
  ["$lvshi__chengmou2"] = "阿瞒，没有我，你得不到冀州啊。",
}


return chengmou